﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Xajhzwb.Components.Helper
{
    public class FileHelper
    {
        public static string PATH_SPLIT_CHAR = "\\";

        public static string GetPathName(string fullPathFileName)
        {
            return Path.GetDirectoryName(fullPathFileName);
        }

        public static string GetFileName(string fullPathFileName)
        {
            return Path.GetFileName(fullPathFileName);
        }

        public static string GetBaseName(string fullPathFileName)
        {
            return Path.GetFileNameWithoutExtension(fullPathFileName);
        }

        public static string GetIdtfName(string fullPathFileName)
        {
            return Path.GetExtension(fullPathFileName);
        }

        public static string GetFullPathFileName(string pathName, string fileName)
        {
            return GetFullPathFileName(pathName, fileName, @"\");
        }

        public static string GetFullPathFileName(string pathName, string fileName, string pathChar)
        {
            string str = "";
            if(pathName == "")
            {
                str = fileName;
            }
            else if(pathName.Substring(pathName.Length - 1, 1) == @"/")
            {
                str = pathName + fileName;
            }
            else if(pathName.Substring(pathName.Length - 1, 1) == @"\")
            {
                str = pathName + fileName;
            }
            else
            {
                str = pathName + pathChar + fileName;
            }
            return str;
        }

        public static long GetFileSize(string fullPathFileName)
        {
            return new FileInfo(fullPathFileName).Length;
        }

        public static DateTime GetFileLastTime(string fullPathFileName)
        {
            return new FileInfo(fullPathFileName).LastWriteTime;
        }

        public static void CreateDirectory(string targetDir)
        {
            if(!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }
        }

        /// <summary>
        /// get file list
        /// </summary>
        /// <param name="searchPath"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public static List<FileInfo> GetFiles(string searchPath, string searchPattern, System.IO.SearchOption searchOption)
        {
            List<FileInfo> fileInfoList = new List<FileInfo>();

            string newSearchPattern = searchPattern.Trim().Replace(".", @"\.").Replace("*", @".*").Replace("?", @".?");
            Regex newSearchPatternRex = new Regex("^" + newSearchPattern + "$", RegexOptions.IgnoreCase);

            DirectoryInfo dirInfo = new DirectoryInfo(searchPath);
            if(dirInfo.Exists)
            {
                FileInfo[] arrFileInfo = dirInfo.GetFiles("*", searchOption);

                foreach(FileInfo fileInfo in arrFileInfo)
                {
                    if(newSearchPatternRex.IsMatch(fileInfo.Name))
                    {
                        fileInfoList.Add(fileInfo);
                    }
                }
            }

            return fileInfoList;
        }

    }
}
